package com.harzone.commons.search.impl.statement;

import org.apache.solr.client.solrj.SolrQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseParser {

	protected SolrQuery query = new SolrQuery("*:*");
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	public SolrQuery getQuery() {
		return query;
	}

	public void setQuery(SolrQuery query) {
		this.query = query;
	}

	/**
	 * 转义特殊字符
	 * 
	 * @author ls 2014年11月26日下午3:04:38
	 * @param s
	 * @return
	 */
	public String escapeQueryChars(String s) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			// These characters are part of the query syntax and must be escaped
			if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '('
					|| c == ')' || c == ':' || c == '^' || c == '[' || c == ']'
					|| c == '\"' || c == '{' || c == '}' || c == '~'
					|| c == '|' || c == '&' || c == ';' || c == '/'
					// || c == '*' || c == '?'
					|| Character.isWhitespace(c)) {
				sb.append("\\");
//				c = '?';
			}
			sb.append(c);
		}
		return sb.toString();
	}
}
